<?php
	class News_Model extends CI_Model {
		function __construct()
		{
			parent::__construct();	
		}
		
		function list_news($p=1,$limit=50)
		{
			if($this->session->userdata('admin_logged_in') == TRUE){
				$start = ($p-1)*50;
				$this->db->where('n_active', 1); 
				$this->db->order_by("n_start", "desc");
				$this->db->limit($limit , $start);
				$this->db->from('news');
				$query = $this->db->get();
				return $query->result();
			}
		}
		
		function list_month($s,$f)
		{
			$where = "( (n_start >= $s AND  n_start <= $f) OR (n_finish >= $s AND  n_finish <= $f) )";
			$this->db->where($where); 
			$this->db->order_by("n_start", "ASC");
			$this->db->limit(4);
			$this->db->from('news');
			$query = $this->db->get();
			return $query->result();
		}
		
		function add_news($input)
		{
			if($this->session->userdata('admin_logged_in') == TRUE){
				if($input['title'] != null) {
					$data = array(
						'n_title' => $input['title'] ,
						'n_start' => strtotime($input['s_date'].' 00:00:00'),
						'n_finish' => strtotime($input['f_date'].' 23:59:59')
					); 
					$this->db->insert('news', $data);
				}
			}
		}
		
		function update_news($input)
		{
			if($this->session->userdata('admin_logged_in') == TRUE){
				if($input['n_id'] != null) {
					$data = array(
						'n_title' => $input['title'] ,
						'n_start' => strtotime($input['s_date'].' 00:00:00'),
						'n_finish' => strtotime($input['f_date'].' 23:59:59')
					);
					$this->db->where('n_id', $input['n_id']);
					$this->db->update('news', $data);
				}
			}
		}
		
		function delete_news($n_id)
		{
			if($this->session->userdata('admin_logged_in') == TRUE){
				$this->db->where('n_id', $n_id); 
				$this->db->delete('news');
			}
		}
		
		function change_status($n_id)
		{
			if($this->session->userdata('admin_logged_in') == TRUE){
				$data = array(
					'n_active' => 'ABS(n_active-1)'
				);
				$this->db->where('n_id', $n_id);
				$this->db->update('news', $data);
			}
		}
		
		function get_news_info($n_id)
		{
			if($this->session->userdata('admin_logged_in') == TRUE){
				$this->db->where('n_id', $n_id);
				$this->db->from('news');
				$query = $this->db->get();
				return $query->result();
			}
		}
	}


/* End of file news_model.php */
/* Location: ./application/models/admin/news_model.php */